package co.edu.uniandes.sar.beans;

import co.edu.uniandes.sar.beans.util.JsfUtil;
import co.edu.uniandes.sar.beans.util.PaginationHelper;
import co.edu.uniandes.sar.ejbs.DirectorDesarrolloFacade;
import co.edu.uniandes.sar.entidades.Directordesarrollo;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;

@ManagedBean(name = "directorDesarrolloBean")
@SessionScoped
public class DirectorDesarrolloBean implements Serializable {

    private Directordesarrollo current;
    private DataModel items = null;
    @EJB
    private co.edu.uniandes.sar.ejbs.DirectorDesarrolloFacade ejbFacade;
    private PaginationHelper pagination;
    private int selectedItemIndex;

    public DirectorDesarrolloBean() {
    }

    public Directordesarrollo getSelected() {
        if (current == null) {
            current = new Directordesarrollo();
            selectedItemIndex = -1;
        }
        return current;
    }

    private DirectorDesarrolloFacade getFacade() {
        return ejbFacade;
    }

    public PaginationHelper getPagination() {
        if (pagination == null) {
            pagination = new PaginationHelper(10) {

                @Override
                public int getItemsCount() {
                    return getFacade().count();
                }

                @Override
                public DataModel createPageDataModel() {
                    return new ListDataModel(getFacade().findRange(new int[]{getPageFirstItem(), getPageFirstItem() + getPageSize()}));
                }
            };
        }
        return pagination;
    }

    private void ejecutarBorrado() {
        try {
            getFacade().remove(current);
            JsfUtil.addSuccessMessage("Se ha borrado correctamente");
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, "Un error ha ocurrido");
        }
    }

    public DataModel getItems() {
        if (items == null) {
            items = getPagination().createPageDataModel();
        }
        return items;
    }

    private void recrearModelo() {
        items = null;
    }

    

    public SelectItem[] getItemsAvailableSelectMany() {
        return JsfUtil.getSelectItems(ejbFacade.findAll(), false);
    }

    public SelectItem[] getItemsAvailableSelectOne() {
        return JsfUtil.getSelectItems(ejbFacade.findAll(), true);
    }

    @FacesConverter(forClass = Directordesarrollo.class)
    public static class DirectordesarrolloBeanConverter implements Converter {

        public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
            if (value == null || value.length() == 0) {
                return null;
            }
            DirectorDesarrolloBean controller = (DirectorDesarrolloBean) facesContext.getApplication().getELResolver().
                    getValue(facesContext.getELContext(), null, "directorDesarrolloBean");
            return controller.ejbFacade.find(getKey(value));
        }

        java.lang.Integer getKey(String value) {
            java.lang.Integer key;
            key = Integer.valueOf(value);
            return key;
        }

        String getStringKey(java.lang.Integer value) {
            StringBuffer sb = new StringBuffer();
            sb.append(value);
            return sb.toString();
        }

        public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
            if (object == null) {
                return null;
            }
            if (object instanceof Directordesarrollo) {
                Directordesarrollo o = (Directordesarrollo) object;
                return getStringKey(o.getIdDirectorDesarrollo());
            } else {
                throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + DirectorDesarrolloBean.class.getName());
            }
        }
    }
}
